package com.alili.springbooteditor.dao;

import com.alili.springbooteditor.entities.editors;
import com.alili.springbooteditor.repository.EditorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.*;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EditorsDao {
    @Autowired
    EditorRepository editorRepository;
    public void save(editors editor){
        editorRepository.save(editor);
    }
    public List<editors> getAllAticle(){
        return editorRepository.findAll();
    }
    public editors getOneArticle(Integer id){
        return editorRepository.findDistinctById(id);
    }
   public Page<editors> findAllEditorsAndSortByDate(Integer pageNum){
       PageRequest pageable = PageRequest.of(pageNum, 4, Sort.Direction.DESC, "editorDate");
       Page<editors> p = editorRepository.findAll(pageable);//按照日期排序显示
       return p;
   }
    public Page<editors> findAllEditorsAndSortById(Integer pageNum){
        PageRequest pageable = PageRequest.of(pageNum, 4, Sort.Direction.DESC, "id");
        Page<editors> p = editorRepository.findAll(pageable);//按照id排序显示
        return p;
    }
    public void deleteE(Integer id){
        editorRepository.deleteById(id);
    }
    public Page<editors> findEditorByTitleAndSortByDate(Integer pageNum,String s){
        PageRequest pageable = PageRequest.of(pageNum, 4,Sort.Direction.DESC, "editorDate");
        editors editor=new editors();
        editor.setEditorTitle(s);
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withMatcher("editorTitle", ExampleMatcher.GenericPropertyMatchers.contains());
        Example<editors> example=Example.of(editor,matcher);//按照文章标题查找，然后按照日期排序显示
        Page<editors> p = editorRepository.findAll(example,pageable);
        return p;
    }

}
